package com.orhon.smartcampus.modules.course.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlModel.*;
import com.orhon.smartcampus.modules.base.mm.*;
import com.orhon.smartcampus.modules.course.dao.CoursesDao;
import com.orhon.smartcampus.modules.course.entity.Courses;
import com.orhon.smartcampus.modules.course.gqlModel.GQLCourses;
import com.orhon.smartcampus.modules.course.mm.CoursesMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class CoursesFetcher implements DataFetcher {

    @Autowired
    CoursesDao coursesDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        List<GQLCourses> courses = new ArrayList<>();

        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);

        String hql = "from Courses where deleted_at = null and school_id = "+schoolId;

        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("id")!=null) {
                hql += " and id " + Util.decide(map.get("id"));
            }
            if(map.get("courseSlug")!=null) {
                hql += " and  course_slug  " + Util.decide(map.get("courseSlug"));
            }
            if(map.get("courseName")!=null) {
                hql += " and course_name "+ Util.decide(map.get("courseName"));
            }
            if(map.get("courseCode")!=null) {
                hql += " and course_code " + Util.decide(map.get("courseCode"));
            }
            if(map.get("type")!=null) {
                hql += " and type  "+Util.decide(map.get("type"));
            }
            if(map.get("subjectId")!=null) {
                hql += " and subject_id " + Util.decide(map.get("subjectId"));
            }
            if(map.get("courseKindDict")!=null) {
                hql += " and course_kind_dict  " +Util.decide(map.get("courseKindDict"));
            }
            if(map.get("level")!=null) {
                hql += " and level " + Util.decide(map.get("level"));
            }
        }
        
        List<Courses>  list = coursesDao.find(hql,null,page.getPage(),page.getLimit());
       
        for (Courses d : list) {
            GQLDictionary gqlDictionary = null;
            if (env.getSelectionSet().contains("data/level")){
                Dictionary dictionary = d.getLevel();
                gqlDictionary = DictionaryMapper.MAPPER.toDictionary(dictionary);
            }
            GQLDictionary gqlDictionary1 = null;
            if (env.getSelectionSet().contains("data/courseKindDict")){
                Dictionary dictionary = d.getCourseKindDict();
                gqlDictionary1 = DictionaryMapper.MAPPER.toDictionary(dictionary);
            }
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("data/school")){
                Schools schools = d.getSchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            GQLSubject gqlSubject = null;
            if (env.getSelectionSet().contains("data/subject")){
                Subjects subjects = d.getSubjectId();
                gqlSubject = SubjectMapper.MAPPER.toSubject(subjects);
            }
            GQLCourses gqlCourses = CoursesMapper.MAPPER.toCourses(d);
            gqlCourses.setCourseKindDict(gqlDictionary1);
            gqlCourses.setLevel(gqlDictionary);
            gqlCourses.setSchool(gqlSchool);
            gqlCourses.setSubject(gqlSubject);
            courses.add(gqlCourses);
        }
        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
        	total = coursesDao.getTotalCount(hql);
        }

        return Result.ok().put("data", courses).put("total", total);
    }
   

}
